/*
ont-space - The ontology-based resource metadata repository
Copyright (c) 2006-2008, Information Eng. Research Unit - Univ. of Alcalá
http://www.cc.uah.es/ie
This library is free software; you can redistribute it and/or modify it under
the terms of the GNU Lesser General Public License as published by the Free
Software Foundation; either version 2.1 of the License, or (at your option)
any later version.
This library is distributed in the hope that it will be useful, but WITHOUT
ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
details.
You should have received a copy of the GNU Lesser General Public License along
with this library; if not, write to the Free Software Foundation, Inc.,
59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
 */
package org.ontspace.util.translator.sld2owl.owl;

import org.w3c.dom.DOMException;
import org.w3c.dom.NamedNodeMap;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import java.util.ArrayList;
import java.util.Iterator;

import com.hp.hpl.jena.ontology.*;
import com.hp.hpl.jena.rdf.model.RDFNode;
import com.hp.hpl.jena.datatypes.RDFDatatype;
import com.hp.hpl.jena.datatypes.TypeMapper;

/**
 * This class translate the components element.This elements could be 
 * environments, roles or activities 
 */
public class ComponentsTranslator {

    lastInstanceSLDTranslator lastInst =
            new lastInstanceSLDTranslator();

    /**
     * This method read all the learners elements
     *@param parent The parent class of the current element
     *@param node The learner node of the Dom structure
     *@param ont The ontology
     *@param instLD The role instance
     *@param nsName The namespace
     *
     */
    private void readLearners(Individual learnerParent, OntClass parent,
            Node node, OntModel ont, Individual instLD, String nsName) {
        /**Method which store the learner instances on recursive form    **/
        DatatypeProperty title =
                ont.getDatatypeProperty(nsName + "title");
        DatatypeProperty identifier =
                ont.getDatatypeProperty(nsName + "identifier");
        DatatypeProperty createNew =
                ont.getDatatypeProperty(nsName + "create-new");
        DatatypeProperty href =
                ont.getDatatypeProperty(nsName + "uri");
        DatatypeProperty matchPersons =
                ont.getDatatypeProperty(nsName + "match-persons");
        DatatypeProperty maxPersons =
                ont.getDatatypeProperty(nsName + "max-persons");
        DatatypeProperty minPersons =
                ont.getDatatypeProperty(nsName + "min-persons");
        ObjectProperty informationProperty =
                ont.getObjectProperty(nsName + "information");
        DatatypeProperty superLearner =
                ont.getDatatypeProperty(nsName + "super-learner");
        DatatypeProperty subLearner =
                ont.getDatatypeProperty(nsName + "sub-learner");
        ObjectProperty roleRef =
                ont.getObjectProperty(nsName + "role-ref");
        ObjectProperty componentRef =
                ont.getObjectProperty(nsName + "component-ref");

        Individual instLe =
                null;

        NodeList learnerChildren =
                node.getChildNodes();

        if (learnerChildren != null) {
            for (int i =
                    0; i < learnerChildren.getLength(); i++) {
                if (learnerChildren.item(i).getNodeName().compareTo("imsld:learner") !=
                        0) {
                    if (learnerChildren.item(i).getNodeName().compareTo("#text") !=
                            0) {

                        String instance =
                                "learner_instance_" +
                                (String.valueOf(lastInst.lastInstance(parent)));
                        instLe =
                                ont.createIndividual(nsName + instance, parent);
                        try {
                            if (learnerParent == null) {
                                instLD.setPropertyValue(roleRef, instLe);
                                instLD.setPropertyValue(componentRef, instLe);
                            }
                            if (learnerParent != null) {

                                RDFNode learnerValue =
                                        ont.createTypedLiteral(instance);
                                learnerParent.setPropertyValue(subLearner,
                                        learnerValue);
                                RDFNode superLearnerValue =
                                        ont.createTypedLiteral(learnerParent.getPropertyValue(identifier).
                                        toString());
                                instLe.setPropertyValue(superLearner,
                                        superLearnerValue);
                            }
                        } catch (Exception e) {

                            e.printStackTrace();
                        }

                        NamedNodeMap attrsItem =
                                node.getAttributes();
                        for (int x =
                                0; x < attrsItem.getLength(); x++) {
                            Node attr =
                                    attrsItem.item(x);
                            if (attr.getNodeName().trim().compareTo("identifier") ==
                                    0) {
                                TypeMapper tm =
                                        TypeMapper.getInstance();
                                RDFDatatype overType =
                                        tm.getSafeTypeByName("http://www.w3.org/2001/XMLSchema#ID");
                                RDFNode identifierValue =
                                        ont.createTypedLiteral(attr.getNodeValue().
                                        trim(), overType);
                                instLe.setPropertyValue(identifier,
                                        identifierValue);
                            }
                            if (attr.getNodeName().trim().compareTo("create-new") ==
                                    0) {
                                RDFNode createnewValue =
                                        ont.createTypedLiteral(attr.getNodeValue().
                                        trim());
                                instLe.setPropertyValue(createNew,
                                        createnewValue);

                            }
                            if (attr.getNodeName().trim().compareTo("href") == 0) {
                                RDFNode hrefValue =
                                        ont.createTypedLiteral(attr.getNodeValue().
                                        trim());
                                instLe.setPropertyValue(href, hrefValue);
                            }
                            if (attr.getNodeName().trim().compareTo("match-persons") ==
                                    0) {
                                RDFNode matchValue =
                                        ont.createTypedLiteral(attr.getNodeValue().
                                        trim());
                                instLe.setPropertyValue(matchPersons, matchValue);
                            }
                            if (attr.getNodeName().trim().compareTo("max-persons") ==
                                    0) {
                                TypeMapper tm =
                                        TypeMapper.getInstance();
                                RDFDatatype overType =
                                        tm.getSafeTypeByName("http://www.w3.org/2001/XMLSchema#nonNegativeInteger");
                                RDFNode maxPersonsValue =
                                        ont.createTypedLiteral(attr.getNodeValue().
                                        trim(), overType);
                                instLe.setPropertyValue(maxPersons,
                                        maxPersonsValue);
                            }
                            if (attr.getNodeName().trim().compareTo("min-persons") ==
                                    0) {
                                TypeMapper tm =
                                        TypeMapper.getInstance();
                                RDFDatatype overType =
                                        tm.getSafeTypeByName("http://www.w3.org/2001/XMLSchema#nonNegativeInteger");
                                RDFNode minPersonsValue =
                                        ont.createTypedLiteral(attr.getNodeValue().
                                        trim(), overType);
                                instLe.setPropertyValue(minPersons,
                                        minPersonsValue);
                            }
                        }//for               
                        if (learnerChildren.item(i).getNodeName().compareTo("imsld:title") ==
                                0) {
                            RDFNode titleValue =
                                    ont.createTypedLiteral(learnerChildren.item(i).
                                    getTextContent());
                            instLe.setPropertyValue(title, titleValue);
                        }
                        if (learnerChildren.item(i).getNodeName().compareTo("imsld:information") ==
                                0) {
                            OntClass information =
                                    ont.getOntClass(nsName + "Information");

                            DatatypeProperty identifierRef =
                                    ont.getDatatypeProperty(nsName +
                                    "identifier-ref");
                            DatatypeProperty visible =
                                    ont.getDatatypeProperty(nsName +
                                    "is-visible");
                            ObjectProperty metadataRef =
                                    ont.getObjectProperty(nsName +
                                    "metadata-ref");


                            Individual instInf =
                                    null;
                            NodeList inforChildren =
                                    learnerChildren.item(i).getChildNodes();
                            lastInst =
                                    new lastInstanceSLDTranslator();
                            instance =
                                    "information_instance_" +
                                    (String.valueOf(lastInst.lastInstance(information)));
                            instInf =
                                    ont.createIndividual(nsName + instance,
                                    information);
                            instLe.setPropertyValue(informationProperty, instInf);

                            for (int x =
                                    0; x < inforChildren.getLength(); x++) {
                                try {


                                    if (inforChildren.item(x).getNodeName().
                                            compareTo("imsld:title") == 0) {
                                        RDFNode inforTitle =
                                                ont.createTypedLiteral(inforChildren.item(x).
                                                getNodeValue());
                                        instInf.setPropertyValue(title,
                                                inforTitle);

                                    }
                                } catch (DOMException ex) {
                                    ex.printStackTrace();
                                }
                                if (inforChildren.item(x).getNodeName().
                                        compareTo("imsld:item") == 0) {
                                    NamedNodeMap attrsInf =
                                            inforChildren.item(x).getAttributes();
                                    for (int y =
                                            0; y < attrsInf.getLength(); y++) {
                                        Node attr =
                                                attrsInf.item(y);

                                        try {
                                            if (attr.getNodeName().trim().
                                                    compareTo("identifierref") ==
                                                    0) {
                                                RDFNode identifierRefValue =
                                                        ont.createTypedLiteral(attr.getNodeValue().
                                                        trim());
                                                instInf.setPropertyValue(identifierRef,
                                                        identifierRefValue);

                                            }
                                            if (attr.getNodeName().trim().
                                                    compareTo("identifier") == 0) {
                                                TypeMapper tm =
                                                        TypeMapper.getInstance();
                                                RDFDatatype overType =
                                                        tm.getSafeTypeByName("http://www.w3.org/2001/XMLSchema#ID");
                                                RDFNode identifierValue =
                                                        ont.createTypedLiteral(attr.getNodeValue().
                                                        trim(), overType);
                                                instInf.setPropertyValue(identifier,
                                                        identifierValue);

                                            }
                                            if (attr.getNodeName().trim().
                                                    compareTo("is-visible") == 0) {
                                                RDFNode visibleValue;
                                                if (attr.getNodeValue().trim().
                                                        equals("true")) {
                                                    visibleValue =
                                                            ont.createTypedLiteral(true);
                                                } else {
                                                    visibleValue =
                                                            ont.createTypedLiteral(false);
                                                }

                                                instInf.setPropertyValue(visible,
                                                        visibleValue);
                                            }
                                            if (attr.getNodeName().trim().
                                                    compareTo("metadata-LOM-ref") ==
                                                    0) {
                                                TypeMapper tm =
                                                        TypeMapper.getInstance();
                                                RDFDatatype overType =
                                                        tm.getSafeTypeByName("http://www.w3.org/2001/XMLSchema#Metadata");
                                                RDFNode metadataValue =
                                                        ont.createTypedLiteral(attr.getNodeValue().
                                                        trim(), overType);
                                                instInf.setPropertyValue(metadataRef,
                                                        metadataValue);
                                            }
                                        } catch (DOMException ex) {
                                            ex.printStackTrace();
                                        }
                                    }//end-for
                                }//end-if item
                            }//end- for inforChildren
                        }//end if information             

                    }
                }//end if !=learner
                if (learnerChildren.item(i).getNodeName().compareTo("imsld:learner") ==
                        0) {
                    readLearners(instLe, parent, learnerChildren.item(i), ont,
                            instLD, nsName);
                }
            }
        }
    }

    /**
     * This method read all the staffs elements
     *@param The parent class of the current element
     *@param The staff node of the Dom structure
     *@param The ontology
     *@param The role instance
     *@param The identifier of the learning design   
     *
     */
    private void readStaffs(Individual staffParent, OntClass parent, Node node,
            OntModel ont, Individual instLD, String nsName) {

        /**Method which store the staff instances on recursive form    **/
        OntClass Staff =
                ont.getOntClass(nsName + "Staff");
        DatatypeProperty title =
                ont.getDatatypeProperty(nsName + "title");
        DatatypeProperty identifier =
                ont.getDatatypeProperty(nsName + "identifier");
        DatatypeProperty createNew =
                ont.getDatatypeProperty(nsName + "create-new");
        DatatypeProperty href =
                ont.getDatatypeProperty(nsName + "href");
        DatatypeProperty matchPersons =
                ont.getDatatypeProperty(nsName + "match-persons");
        DatatypeProperty maxPersons =
                ont.getDatatypeProperty(nsName + "max-persons");
        DatatypeProperty minPersons =
                ont.getDatatypeProperty(nsName + "min-persons");
        ObjectProperty informationRef =
                ont.getObjectProperty(nsName + "information-ref");
        DatatypeProperty subStaff =
                ont.getDatatypeProperty(nsName + "sub-staff");
        DatatypeProperty superStaff =
                ont.getDatatypeProperty(nsName + "super-staff");


        NodeList staffChildren =
                node.getChildNodes();
        Individual instLe =
                null;
        if (staffChildren != null) {
            for (int i =
                    0; i < staffChildren.getLength(); i++) {

                if (staffChildren.item(i).getNodeName().compareTo("imsld:staff") !=
                        0) {
                    if (staffChildren.item(i).getNodeName().compareTo("#text") !=
                            0) {
                        lastInst =
                                new lastInstanceSLDTranslator();
                        String instance =
                                "staff_instance_" +
                                (String.valueOf(lastInst.lastInstance(parent)));

                        try {

                            instLe =
                                    ont.createIndividual(nsName + instance,
                                    parent);

                            if (staffParent == null) {
                                OntClass learningDes =
                                        ont.getOntClass(nsName +
                                        "Learning-Design");
                                ObjectProperty roleRef =
                                        ont.getObjectProperty(nsName +
                                        "inverse_of_role-ref");
                                instLD.setPropertyValue(roleRef, instLe);
                            }
                            if (staffParent != null) {
                                RDFNode staffValue =
                                        ont.createTypedLiteral(instance);
                                staffParent.setPropertyValue(subStaff,
                                        staffValue);
                                RDFNode superStaffValue =
                                        ont.createTypedLiteral(staffParent.getPropertyValue(identifier).
                                        toString());
                                instLe.setPropertyValue(superStaff,
                                        superStaffValue);
                            }
                        } catch (Exception ex) {
                            ex.printStackTrace();
                        }

                        NamedNodeMap attrsItem =
                                node.getAttributes();
                        for (int x =
                                0; x < attrsItem.getLength(); x++) {
                            Node attr =
                                    attrsItem.item(x);
                            if (attr.getNodeName().trim().compareTo("identifier") ==
                                    0) {
                                TypeMapper tm =
                                        TypeMapper.getInstance();
                                RDFDatatype overType =
                                        tm.getSafeTypeByName("http://www.w3.org/2001/XMLSchema#ID");
                                RDFNode identifierValue =
                                        ont.createTypedLiteral(attr.getNodeValue().
                                        trim(), overType);
                                instLe.setPropertyValue(identifier,
                                        identifierValue);
                            }
                            if (attr.getNodeName().trim().compareTo("create-new") ==
                                    0) {
                                RDFNode createNewValue =
                                        ont.createTypedLiteral(attr.getNodeValue().
                                        trim());
                                instLe.setPropertyValue(createNew,
                                        createNewValue);
                            }
                            if (attr.getNodeName().trim().compareTo("href") == 0) {
                                RDFNode hrefValue =
                                        ont.createTypedLiteral(attr.getNodeValue().
                                        trim());
                                instLe.setPropertyValue(href, hrefValue);
                            }
                            if (attr.getNodeName().trim().compareTo("match-persons") ==
                                    0) {
                                RDFNode matchPersonsValue =
                                        ont.createTypedLiteral(attr.getNodeValue().
                                        trim());
                                instLe.setPropertyValue(matchPersons,
                                        matchPersonsValue);
                            }
                            if (attr.getNodeName().trim().compareTo("max-persons") ==
                                    0) {
                                TypeMapper tm =
                                        TypeMapper.getInstance();
                                RDFDatatype overType =
                                        tm.getSafeTypeByName("http://www.w3.org/2001/XMLSchema#nonNegativeInteger");
                                RDFNode maxPersonsValue =
                                        ont.createTypedLiteral(attr.getNodeValue().
                                        trim(), overType);
                                instLe.setPropertyValue(maxPersons,
                                        maxPersonsValue);
                            }
                            if (attr.getNodeName().trim().compareTo("min-persons") ==
                                    0) {
                                TypeMapper tm =
                                        TypeMapper.getInstance();
                                RDFDatatype overType =
                                        tm.getSafeTypeByName("http://www.w3.org/2001/XMLSchema#nonNegativeInteger");
                                RDFNode minPersonsValue =
                                        ont.createTypedLiteral(attr.getNodeValue().
                                        trim(), overType);
                                instLe.setPropertyValue(minPersons,
                                        minPersonsValue);
                            }

                        }//for               
                        if (staffChildren.item(i).getNodeName().compareTo("imsld:title") ==
                                0) {
                            String leTitle =
                                    staffChildren.item(i).getTextContent();
                            RDFNode titleValue =
                                    ont.createTypedLiteral(leTitle);
                            instLe.setPropertyValue(title, titleValue);

                        }
                        if (staffChildren.item(i).getNodeName().
                                compareTo("imsld:information") == 0) {
                            OntClass information =
                                    ont.getOntClass(nsName + "Information");
                            OntClass item =
                                    ont.getOntClass(nsName + "Item");

                            DatatypeProperty identifierRef =
                                    ont.getDatatypeProperty(nsName +
                                    "identifier-ref");
                            DatatypeProperty isVisible =
                                    ont.getDatatypeProperty(nsName + "isvisible");

                            ObjectProperty metadataRef =
                                    ont.getObjectProperty(nsName +
                                    "metadata-ref");

                            Individual instInf =
                                    null;
                            NodeList inforChildren =
                                    staffChildren.item(i).getChildNodes();

                            lastInst =
                                    new lastInstanceSLDTranslator();
                            instance =
                                    "information_instance_c" +
                                    (String.valueOf(lastInst.lastInstance(information)));
                            instInf =
                                    ont.createIndividual(nsName + instance,
                                    information);
                            instLe.setPropertyValue(informationRef, instInf);
                            for (int x =
                                    0; x < inforChildren.getLength(); x++) {

                                try {


                                    if (inforChildren.item(x).getNodeName().
                                            compareTo("imsld:title") == 0) {
                                        //String inforTitle= inforChildren.item(x).getNodeValue();                              
                                        String inforTitle =
                                                inforChildren.item(x).
                                                getChildNodes().item(0).
                                                getNodeValue();

                                        RDFNode titleValue =
                                                ont.createTypedLiteral(inforTitle);
                                        instInf.setPropertyValue(title,
                                                titleValue);

                                    }
                                } catch (DOMException ex) {
                                    ex.printStackTrace();
                                } catch (Exception ex) {
                                    ex.printStackTrace();
                                }
                                if (inforChildren.item(x).getNodeName().
                                        compareTo("imsld:item") == 0) {
                                    NamedNodeMap attrsInf =
                                            inforChildren.item(x).getAttributes();
                                    for (int y =
                                            0; y < attrsInf.getLength(); y++) {
                                        Node attr =
                                                attrsInf.item(y);

                                        try {
                                            if (attr.getNodeName().trim().
                                                    compareTo("identifierref") ==
                                                    0) {
                                                RDFNode identifierRefValue =
                                                        ont.createTypedLiteral(attr.getNodeValue().
                                                        trim());
                                                instInf.setPropertyValue(identifierRef,
                                                        identifierRefValue);

                                            }
                                            if (attr.getNodeName().trim().
                                                    compareTo("identifier") == 0) {

                                                TypeMapper tm =
                                                        TypeMapper.getInstance();
                                                RDFDatatype overType =
                                                        tm.getSafeTypeByName("http://www.w3.org/2001/XMLSchema#ID");
                                                RDFNode identifierValue =
                                                        ont.createTypedLiteral(attr.getNodeValue().
                                                        trim(), overType);
                                                instInf.setPropertyValue(identifier,
                                                        identifierValue);
                                            }
                                            if (attr.getNodeName().trim().
                                                    compareTo("is-visible") == 0) {
                                                RDFNode isVisibleValue;
                                                if (attr.getNodeValue().trim().
                                                        equals("true")) {
                                                    isVisibleValue =
                                                            ont.createTypedLiteral(true);
                                                } else {
                                                    isVisibleValue =
                                                            ont.createTypedLiteral(false);
                                                }
                                                instInf.setPropertyValue(isVisible,
                                                        isVisibleValue);
                                            }
                                            if (attr.getNodeName().trim().
                                                    compareTo("metadata-LOM-ref") ==
                                                    0) {
                                                TypeMapper tm =
                                                        TypeMapper.getInstance();
                                                RDFDatatype overType =
                                                        tm.getSafeTypeByName("http://www.w3.org/2001/XMLSchema#Metadata");
                                                RDFNode metadataRefValue =
                                                        ont.createTypedLiteral(attr.getNodeValue().
                                                        trim(), overType);
                                                instInf.setPropertyValue(metadataRef,
                                                        metadataRefValue);

                                            }
                                        } catch (DOMException ex) {
                                            ex.printStackTrace();
                                        } catch (Exception ex) {
                                            ex.printStackTrace();
                                        }
                                    }//end-for
                                }//end-if item
                            }//end- for inforChildren
                        }//end if information             

                    }
                }//end if !=staff
                if (staffChildren.item(i).getNodeName().compareTo("imsld:staff") ==
                        0) {
                    readStaffs(instLe, parent, staffChildren.item(i), ont,
                            instLD, nsName);
                }
            }
        }
    }

    /** 
     * Creates a new instance of a component
     * When the element reeded is a component. The classes can be:
     *       Environments
     *      Roles
     *          Learner
     *          Staff
     *      Activities
     *          LearningActivity
     *          SupportActivity
     *          ActivityStructure
     * @param lnode The dom node of the component
     * @param ont The ontology
     * @param instLd The learning design instance
     * @param nsName The namespace
     **/
    public ComponentsTranslator(Node lnode, OntModel ont, Individual instLd,
            String nsName) {

        OntClass role =
                ont.getOntClass(nsName + "Role");
        OntClass learner =
                ont.getOntClass(nsName + "Learner");
        OntClass staff =
                ont.getOntClass(nsName + "Staff");
        OntClass learningActivity =
                ont.getOntClass(nsName + "Learning-Activity");
        OntClass supportActivity =
                ont.getOntClass(nsName + "Support-Activity");
        OntClass activityStructure =
                ont.getOntClass(nsName + "Activity-Structure");
        OntClass environment =
                ont.getOntClass(nsName + "Environment");

        int count =
                0;

        refTranslator reference =
                new refTranslator();

        ArrayList<refTranslator> referencesActStruct =
                new ArrayList<refTranslator>();
        ArrayList<refTranslator> referencesEnvironment =
                new ArrayList<refTranslator>();
        ArrayList<refTranslator> referencesSupport =
                new ArrayList<refTranslator>();
        ArrayList<refTranslator> referencesLearningActivity =
                new ArrayList<refTranslator>();

        NodeList children =
                lnode.getChildNodes();
        if (children != null) {
            for (int i =
                    0; i < children.getLength(); i++) {
                if (children.item(i).getNodeName().compareTo("imsld:environments") ==
                        0) {
                    EnvironmentsTranslator de =
                            new EnvironmentsTranslator(environment,
                            children.item(i), ont, instLd, nsName,
                            referencesEnvironment);
                }
                if (children.item(i).getNodeName().compareTo("imsld:roles") == 0) {
                    NodeList roleChildren =
                            children.item(i).getChildNodes();
                    for (int a =
                            0; a < roleChildren.getLength(); a++) {
                        if (roleChildren.item(a).getNodeName().compareTo("imsld:learner") ==
                                0) {
                            readLearners(null, learner, roleChildren.item(a),
                                    ont, instLd, nsName);
                        }
                        if (roleChildren.item(a).getNodeName().compareTo("imsld:staff") ==
                                0) {
                            readStaffs(null, staff, roleChildren.item(a), ont,
                                    instLd, nsName);
                        }
                    }
                }//IF-ROLES                         
                if (children.item(i).getNodeName().compareTo("imsld:activities") ==
                        0) {
                    NodeList activitiesChildren =
                            children.item(i).getChildNodes();
                    for (int x =
                            0; x < activitiesChildren.getLength(); x++) {
                        if (activitiesChildren.item(x).getNodeName().compareTo("imsld:learning-activity") ==
                                0) {
                            LearningActivitiesTranslator dla =
                                    new LearningActivitiesTranslator(learningActivity,
                                    activitiesChildren.item(x), ont, instLd,
                                    nsName, referencesEnvironment);
                        }
                        if (activitiesChildren.item(x).getNodeName().compareTo("imsld:support-activity") ==
                                0) {
                            SupportActivitiesTranslator dsa =
                                    new SupportActivitiesTranslator(supportActivity,
                                    activitiesChildren.item(x), ont, instLd,
                                    nsName, referencesEnvironment);
                        }
                        if (activitiesChildren.item(x).getNodeName().compareTo("imsld:activity-structure") ==
                                0) {
                            ActivityStructuresTranslator das =
                                    new ActivityStructuresTranslator(activityStructure,
                                    activitiesChildren.item(x), ont, instLd,
                                    nsName, referencesActStruct,
                                    referencesEnvironment, referencesSupport,
                                    referencesLearningActivity);
                        }

                    } //for  
                }   //if   


            }
        }
        //Set the value to the property activityStructureRef in the all activity structure instances
        ObjectProperty actStructRef =
                ont.getObjectProperty(nsName + "activity-structure-ref");
        DatatypeProperty identifier =
                ont.getDatatypeProperty(nsName + "identifier");
        Iterator it =
                referencesActStruct.iterator();
        while (it.hasNext()) {
            TypeMapper tm =
                    TypeMapper.getInstance();
            RDFDatatype overType =
                    tm.getSafeTypeByName("http://www.w3.org/2001/XMLSchema#ID");
            refTranslator refe =
                    (refTranslator) it.next();
            searchInstance searchInst =
                    new searchInstance();
            RDFNode instValue =
                    ont.createTypedLiteral(refe.instRef, overType);
            Individual instActivityStructureRef =
                    searchInst.search(activityStructure, identifier, instValue);
            refe.inst.addProperty(actStructRef, instActivityStructureRef);
        }
        //Set the value to the property environmentRef in the learning activities, support activity and activity-structure instances
        ObjectProperty environmentRef =
                ont.getObjectProperty(nsName + "environment-ref");
        it =
                referencesEnvironment.iterator();
        while (it.hasNext()) {
            TypeMapper tm =
                    TypeMapper.getInstance();
            RDFDatatype overType =
                    tm.getSafeTypeByName("http://www.w3.org/2001/XMLSchema#ID");
            refTranslator refe =
                    (refTranslator) it.next();
            searchInstance searchInst =
                    new searchInstance();
            RDFNode instValue =
                    ont.createTypedLiteral(refe.instRef, overType);
            Individual instEnvironmentRef =
                    searchInst.search(environment, identifier, instValue);
            refe.inst.addProperty(environmentRef, instEnvironmentRef);
        }
        //Set the value to the property supportRef in the activity structure instances
        ObjectProperty supportRef =
                ont.getObjectProperty(nsName + "support-activity-ref");
        it =
                referencesSupport.iterator();
        while (it.hasNext()) {
            TypeMapper tm =
                    TypeMapper.getInstance();
            RDFDatatype overType =
                    tm.getSafeTypeByName("http://www.w3.org/2001/XMLSchema#ID");
            refTranslator refe =
                    (refTranslator) it.next();
            searchInstance searchInst =
                    new searchInstance();
            RDFNode instValue =
                    ont.createTypedLiteral(refe.instRef, overType);
            Individual instSupportRef =
                    searchInst.search(supportActivity, identifier, instValue);
            refe.inst.addProperty(supportRef, instSupportRef);
        }


        ObjectProperty learningActivityRef =
                ont.getObjectProperty(nsName + "learning-activity-ref");
        it =
                referencesLearningActivity.iterator();
        while (it.hasNext()) {
            TypeMapper tm =
                    TypeMapper.getInstance();
            RDFDatatype overType =
                    tm.getSafeTypeByName("http://www.w3.org/2001/XMLSchema#ID");
            refTranslator refe =
                    (refTranslator) it.next();
            searchInstance searchInst =
                    new searchInstance();
            RDFNode instValue =
                    ont.createTypedLiteral(refe.instRef, overType);
            Individual instLearningActivityRef =
                    searchInst.search(learningActivity, identifier, instValue);
            refe.inst.addProperty(learningActivityRef, instLearningActivityRef);
        }
    }
}

    